import i18n from '@root/i18n';
world.onPlayerJoin(({ entity: e }) => {
  e.skipLv = 3;
});
world.onPress(async ({ entity, button }) => {
  if (button === 'action1') {
    const sel = await entity.player.dialog({
      type: GameDialogType.SELECT,
      content: i18n.t('now', {
        lng: entity.player.lng,
        time: (Date.now() - entity.startTime) / 1000,
      }),
      options: [
        i18n.t('切换人称', { lng: entity.player.lng }),
        i18n.t('观战', { lng: entity.player.lng }),
        i18n.t('跳关', { lng: entity.player.lng, lv: entity.skipLv }),
      ],
    });
    if (sel) {
      if (sel.value === i18n.t('切换人称', { lng: entity.player.lng })) {
        entity.player.cameraMode =
          entity.player.cameraMode === GameCameraMode.FPS
            ? GameCameraMode.FOLLOW
            : GameCameraMode.FPS;
      } else if (sel.value === i18n.t('观战', { lng: entity.player.lng })) {
        const options = world
          .querySelectorAll('player')
          .filter((e) => e !== entity)
          .map((i) => i.player.name);
        options.unshift('我');
        const sel1 = await entity.player.dialog({
          type: GameDialogType.SELECT,
          content: i18n.t('all_user', {
            lng: entity.player.lng,
            length: options.length,
          }),
          options,
        });
        if (sel1) {
          if (sel1.index === 0) {
            entity.player.cameraEntity = entity;
          } else {
            const other = world
              .querySelectorAll('player')
              .find((i) => i.player.name === sel1.value);
            if (other) {
              entity.player.cameraEntity = other;
            } else {
              entity.player.directMessage(
                i18n.t('玩家已离开', { lng: entity.player.lng })
              );
            }
          }
        }
      } else {
        if (entity.skipLv <= 0) {
          return entity.player.directMessage(
            i18n.t('no_skip', { lng: entity.player.lng })
          );
        }
        let e = null;
        if (entity.point) {
          const next_point = world.querySelector(
            `#旋转圆圈-${Number(entity.point.id.split('-')[1]) + 1}`
          );
          if (next_point) {
            e = next_point;
          }
        } else {
          e = world.querySelector('#旋转圆圈-1');
        }
        if (e) {
          entity.point = e;
          entity.skipLv--;
          entity.position.copy(e.position);
          entity.player.spawnPoint.copy(e.position);
        } else {
          entity.player.directMessage('已无存档可跳');
        }
      }
    }
  }
});
